package com.keshe.springboot_keshe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keshe.springboot_keshe.common.Result;
import com.keshe.springboot_keshe.entity.User;
import com.keshe.springboot_keshe.entity.Zhanghao;
import com.keshe.springboot_keshe.mapper.UserMapper;
import com.keshe.springboot_keshe.mapper.ZhanghaoMapper;
import com.keshe.springboot_keshe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ZhanghaoMapper zhanghaoMapper;

    @Override
    public Result<Zhanghao> login(Zhanghao zhanghao) {
        QueryWrapper<Zhanghao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", zhanghao.getUsername())
                .eq("password", zhanghao.getPassword());
        Zhanghao user = zhanghaoMapper.selectOne(queryWrapper);
        if (user != null) {
            return Result.success(user);
        }
        return Result.error("用户名或密码错误");
    }

    @Override
    public Result getUserList(Map<String, Object> params) {
        // 创建分页对象
        Page<User> page = new Page<>(
                params.get("pageNum") != null ? Long.parseLong(params.get("pageNum").toString()) : 1,
                params.get("pageSize") != null ? Long.parseLong(params.get("pageSize").toString()) : 10
        );
        
        // 构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // 添加查询条件，如果有的话
        if (params.get("userName") != null) {
            queryWrapper.like("user_name", params.get("userName"));
        }
        
        // 执行分页查询
        Page<User> pageResult = userMapper.selectPage(page, queryWrapper);
        
        // 构建返回结果
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageResult.getTotal());
        resultMap.put("list", pageResult.getRecords());
        resultMap.put("pages", pageResult.getPages());
        resultMap.put("current", pageResult.getCurrent());
        
        return Result.success(resultMap);
    }

    @Override
    public Result addUser(User user) {
        if (userMapper.insert(user) > 0) {
            return Result.success();
        }
        return Result.error("添加失败");
    }

    @Override
    public Result updateUser(User user) {
        if (userMapper.updateById(user) > 0) {
            return Result.success();
        }
        return Result.error("修改失败");
    }

    @Override
    public Result deleteUser(Integer id) {
        if (userMapper.deleteById(id) > 0) {
            return Result.success();
        }
        return Result.error("删除失败");
    }

    @Override
    public Result batchDeleteUser(List<Integer> ids) {
        if (userMapper.deleteBatchIds(ids) > 0) {
            return Result.success();
        }
        return Result.error("批量删除失败");
    }
}
   